<?php


class UserContentHelper extends AppHelper {

	var $helpers = array('Html','Session','Javascript');
	var $BlockedItem;
	var $ReportedItem;
	function shareLink($subject)
	{
		$link = "mailto:?subject=".htmlentities($subject)."&body=http://".$_SERVER['HTTP_HOST'].$this->here;
		return $link;
	}
	/**
	 * 
	 * @param $itemId
	 * @param $type
	 * @param $linkTitle
	 * @return unknown_type
	 */
	function addToFavorites($itemId, $type, $linkTitle='Add to favorites',$wrap = true)
	{
		//prd($this);
		$url = am($this->getActionUrl('Favorite'),array($itemId,$type));
		$params = array('type'=>$type ,'itemId'=>$itemId,'url'=>$url);
		$link = $this->ajaxLink(__($linkTitle),$params );
		return $link;
	}
	/**
	 * 
	 * @param $itemId
	 * @param $type
	 * @param $linkTitle
	 * @return unknown_type
	 */
	function removeFavorites($itemId, $type, $linkTitle='Remove from favorites',$wrap = true)
	{
		//prd($this);
		$url = am($this->getActionUrl('RemoveFavorite'),array($itemId,$type));
		$params = array('type'=>$type ,'itemId'=>$itemId,'url'=>$url);
		$link = $this->ajaxLink(__($linkTitle), $params);
		return $link;
	}	
	// move it to ajax helper
	function ajaxLink($title, $params, $wrap = true)
	{
		if(!isset($this->jsAdded))
		{
			$view =& ClassRegistry::getObject('view');
			$this->Javascript->link('/cms/js/jquery.ajaxify.js',false);
			$this->jsAdded = true;
		}
		extract($params);
		$uniqueClass = "favorite_".$type.'_'.$itemId;
		$class = $uniqueClass;
		$link = $this->Html->link($title,$url,array('class'=>$uniqueClass));
		$wrapClass = $uniqueClass."_parent";
		$loadingImage = $this->Html->url('/effects/img/circularAnimation.gif');
		$js =<<< END
		<script type="text/javascript">
			\$(document).ready(function(){
				\$('.$uniqueClass').ajaxify({
					target:".$wrapClass",
					loading_img:false,
				})
			})
		</script>
END;

		$code = $link.$js;
		if($wrap)
		{
			$code = $this->Html->tag('span',$code,array('class'=>"$wrapClass"));
		}
		//$code = '<a href="#" class ="'.$class.'" onclick="return favorite.add('.$jsonData.');">'.$title.'</a>';
		
		return $code;		
	}

	/**
	 * 
	 * @param $itemId
	 * @param $type
	 * @param $addlinkTitle
	 * @param $removelinkTitle
	 * @return unknown_type
	 */
	function toggleFavorites($itemId, $type, $addlinkTitle='Add to favorites',$removelinkTitle='Remove from favorites')
	{
		if(!$this->Session->read('Auth.User'))
			return;
		$userId = $this->Session->read('Auth.User.id');
		if(ClassRegistry::init('Users.UserFavorite')->find('count',array('conditions'=>array('type'=>$type,'item_id'=>$itemId,'user_id'=>$userId)))==0)
		{
			$out =  $this->addToFavorites($itemId, $type,$addlinkTitle);
		}
		else
		{
			$out =  $this->removeFavorites($itemId, $type,$removelinkTitle);
		}
			
		return $out;
	}
	/**
	 * 
	 * @param $itemId
	 * @param $type
	 * @param $addLinkTitle
	 * @param $removeLinkTitle
	 * @param $options
	 * @return unknown_type
	 */
	//@todo- init BlockedItem model. 
	function toggleBlockedItem($itemId,$type,$addLinkTitle = "Block",$removeLinkTitle ="Unblock",$options=array())
	{
		if(!$this->Session->read('Auth.User'))
			return;
		if($this->BlockedItem->isBlocked($type,$itemId,$this->Session->read('Auth.User.id'))==false)
			$out = $this->blockItem($itemId,$type,$addLinkTitle.$options);
		else
			$out = $this->unblockItem($itemId,$type,$removeLinkTitle);
		return $out;
	}
	/**
	 * 
	 * @param $itemId
	 * @param $type
	 * @param $title
	 * @param $options
	 * @return unknown_type
	 */
	function blockItem($itemId,$type,$title,$options=array())
	{
		return $this->Html->link(__('Block',true),array('action'=>'block','controller'=>'user_contents','plugin'=>'users',$type,$itemId),$options);
	}
	/**
	 * 
	 * @param $itemId
	 * @param $type
	 * @param $title
	 * @param $options
	 * @return unknown_type
	 */
	function unblockItem($itemId,$type,$title,$options=array())
	{
		return $this->Html->link(__('Unblock',true),am($this->getActionUrl("UnBlock"),array($type,$itemId)),$options);
	}
	/**
	 * 
	 * @param $entity
	 * @param $entityId
	 * @param $title
	 * @param $options
	 * @return unknown_type
	 */
	function report($entity,$entityId,$title = "Report Violation",$options=array())
	{
		return $this->Html->link(__($title,true),am($this->getActionUrl('Report'),array($entity,$entityId)),$options);
	}
	
	function getActionUrl($action)
	{
		$urls['Block'] = array('action'=>'block','controller'=>'user_contents','plugin'=>'users');
		$urls['UnBlock']  = array('action'=>'unblock','controller'=>'user_contents','plugin'=>'users');
		$urls['Favorite'] = array('action'=>'add','controller'=>'user_favorites','plugin'=>'users');
		$urls['RemoveFavorite'] = array('action'=>'remove','controller'=>'user_favorites','plugin'=>'users');
		$urls['Report'] = array('plugin'=>'users','controller'=>'user_contents','action'=>'report') ;
		return isset($urls[$action]) ? $urls[$action] : array();
	}

}
?>